import router from "../../../../apis/routers";

const key = getFilePathStore(import.meta.url)

export const usePasswordStore = defineStore(key, {
    state: () => ({
        userLoginComponentStore: userUserLoginComponentStore(),
        usernameInputProps: "",
        passwordInputProps: "",
    }),
    actions: {
        async initStore() {
            await this.userLoginComponentStore.initStore();
            this.usernameInputProps = "";
            this.passwordInputProps = "";
        },
        async getThrowMessage() {
            getIsNull(this.usernameInputProps, this.passwordInputProps)
                ? (() => { throw '用户名和密码不能为空' })()
                : (() => {
                    getSomeValue(this.userLoginComponentStore.agreementComponetStore.agree, false)
                        ? (() => { throw '请先同意协议' })()
                        : void 0;
                })();
        },
        async userLogin() {
            try {
                await this.getThrowMessage();
                // TODO: api:用户登录代码实现
                await getUserLoginForUserPwdAPI(this.usernameInputProps, this.passwordInputProps).then((user) => {
                    console.log(user);
                    router.push({name: 'home'});
                });
            } catch (error) {
                console.log(error);
            }
        },
    },
    persist: {
        key: key,
        storage: sessionStorage
    }
})